import os
import time
import uuid

import requests

from common.config import getconf
from tests.ry_park_service.parking_tools import login_token, internal_token


class ParkLeave:

    # =====================车辆进场=====================

    # 补录
    def addEnterRecord(self, env, plate="", enterTime=int(time.time())):
        base_url = getconf(env, "parking_service_url")
        url = base_url + "/v1/park/supmenter"
        headers = {"content-type": "application/json; charset=utf-8", "token": login_token(env)}
        dict = {
            "parkingGateId": getconf(env, "parkingGateId_in"),
            "parkingSentryBoxId": getconf(env, "parkingSentryBoxId"),
            "plate": plate,
            "enterTime": enterTime,
            "carColor": "White",
            "plateColor": "Blue",
            "releaseType": 10,  # 1-自动放行；MANUAL：2-人工开闸；REGISTER：10-补录进场
            "remark": "自动化接口测试补录",
            "serial":str(uuid.uuid1())
        }

        return requests.post(url=url, headers=headers, json=dict).json()

    #查询车辆停车账单信息
    def queryCarOrder(self,env,plate=""):

        base_url = getconf(env, "parking_service_url")
        url = base_url + "/v1/park/queryCarOrder"
        headers = {"content-type": "application/json; charset=utf-8",
                   "token": login_token(env)}
        dict = {
            "parkingGateId": getconf(env,"parkingGateId_out"),
            "parkingSentryBoxId": getconf(env,"parkingSentryBoxId"),
            "plate": plate,
            "carColor": "White",
            "eventId": 0,
            "orderFrontId": 0,
            "plateColor": "Blue",
            "releaseType": 0,
            "serial": str(uuid.uuid1())
        }
        return requests.post(url, headers=headers, json=dict).json()

    # 出场记录上报
    def reportLeave(self, env, plate="", release_type=1,payAmount=0,eventId=0,orderFrontId=0,payAmountCalc=0,orderNo=None):
        base_url = getconf(env, "parking_service_url")
        url = base_url + "/v1/park/leave"
        # headers = {"Content-Type": "multipart/mixed; boundary=b0f0b079-12c9-4337-9b9b-2d6209f0b9a4","token": login_token(env)}
        headers = {"token": login_token(env)}
        dict = {
            "parkingGateId": getconf(env, "parkingGateId_out"),
            "parkingSentryBoxId": getconf(env, "parkingSentryBoxId"),
            "plate": plate,
            "carColor": "White",
            "plateColor": "Blue",
            "releaseType": release_type,  # 1-自动放行；MANUAL：2-人工开闸；REGISTER：10-补录进场
            "payAmount": payAmount,
            "eventId": eventId,
            "orderNo": None,
            "orderFrontId": orderFrontId,
            "payAmountCalc": payAmountCalc,
            "serial": str(uuid.uuid1())

        }
        img_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))), 'img')
        files = {'file': ('park.jpg', open(os.path.join(img_path, 'park.jpg'), 'rb'), 'img/jpg')}
        return requests.post(url=url, headers=headers, data=dict, files=files).json()

    # 查询场内车辆
    def queryPreSentCar(self, env, plate=""):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/v1/presentCar/list/page"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "plate": plate
        }
        result = requests.post(url, headers=headers, json=dict).json()
        if result["code"]=='0' and len(result["data"]["records"]):
            return len(result["data"]["records"])
        else:
            return 0
